package com.dragee.modules.work.controller;

import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.config.exception.BusinessErrorType;
import com.dragee.config.exception.BusinessException;
import com.dragee.constants.CommonConstants;
import com.dragee.modules.sys.entity.LoginUser;
import com.dragee.modules.work.entity.JoinEntity;
import com.dragee.modules.work.entity.TrainEntity;
import com.dragee.modules.work.entity.TrainProjectEntity;
import com.dragee.modules.work.service.JoinService;
import com.dragee.modules.work.service.TrainProjectService;
import com.dragee.modules.work.service.TrainService;
import com.dragee.utils.LoginUserUtil;
import com.dragee.utils.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;



/**
 * 培训报名
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-05-25 15:19:06
 */
@RestController
@RequestMapping("work/join")
@Api(tags = "work - 培训报名")
public class JoinController {
    @Autowired
    private JoinService joinService;
    @Autowired
    private TrainService trainService;
    @Autowired
    private TrainProjectService trainProjectService;

    @Value("${webull.mapUploadPath}")
    private String rootPath;

    private final String file_join_Pay = "trainPay";


    /**
     * 列表
     */
    @GetMapping("list")
    @ApiOperation("培训报名列表")
    public R list(@RequestParam Map<String, Object> params){
        LoginUser user = LoginUserUtil.getLoginUser();
        params.put("createBy", user.getName());
        PageUtils page = joinService.queryPage(params);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("培训报名信息")
    public R info(@PathVariable("id") Long id){
		JoinEntity join = joinService.getById(id);

        return R.ok().put("data", join);
    }

    /**
     * 保存
     */
    @PostMapping("saveOrUpdate")
    @ApiOperation("保存或修改培训报名")
    public R saveOrUpdate(@RequestBody JoinEntity join){
        TrainEntity train = trainService.getById(join.getTrainId());
        TrainProjectEntity project = trainProjectService.getById(join.getProjectId());
        BigDecimal cost = new BigDecimal(0);
        if (project.getEntryPrice() != null) {
            cost.add(project.getEntryPrice());
            join.setEntryCost(project.getEntryPrice());
        }
        if (join.getRoomType().equals("标间")) {
            cost.add(train.getStandardRoomPrice());
        } else if (join.getRoomType().equals("单间")) {
            cost.add(train.getSingleRoomPrice());
        }
        join.setCost(cost);
        join.setStatus("草稿");
        joinService.saveOrUpdate(join);
        return R.ok();
    }


    /**
     * 删除
     */
    @GetMapping("delete-by-id")
    @ApiOperation("培训报名删除")
    public R delete( String id){
		joinService.removeById(Long.parseLong(id));
        return R.ok();
    }

    /**
     * 删除
     */
    @GetMapping("submitJoin")
    @ApiOperation("报名提交")
    public R submitJoin(@RequestParam("id") String id){
        JoinEntity joinEntity = joinService.getById(id);
        joinEntity.setStatus("待审核");
        joinEntity.setJoinTime(new Date());
        joinService.saveOrUpdate(joinEntity);
        return R.ok();
    }


    /**
     * 列表
     */
    @PostMapping("uploadPayPic")
    @ApiOperation("上传缴费凭证")
    public R uploadPayPic(MultipartFile file, String id) throws Exception {
        if (file == null || id == null) {
            throw new BusinessException(BusinessErrorType.INVALID_PARAMS);
        }
        String path = rootPath + CommonConstants.PIC_DIR + "/" + LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE);
        UploadUtil.uploadOne(file, path, file_join_Pay, Long.parseLong(id));
        return R.ok();
    }


}
